package com.cjj.common.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帖子分类枚举
 */
public enum PostCategory {
    TECHNOLOGY("TECHNOLOGY", "技术交流"),
    STUDY_MATERIAL("STUDY_MATERIAL", "学习资料"),
    ELECTRONIC_PRODUCTS("ELECTRONIC_PRODUCTS", "电子产品"),
    DAILY_QUESTION("DAILY_QUESTION", "日常问答"),
    CAMPUS_LIFE("CAMPUS_LIFE", "校园生活"),
    JOB_INTERNSHIP("JOB_INTERNSHIP", "求职实习"),
    OTHER("OTHER", "其他");
    
    private final String value;
    private final String desc;
    
    PostCategory(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }
    
    public String getValue() {
        return value;
    }
    
    public String getDesc() {
        return desc;
    }
    
    /**
     * 通过值查找枚举
     */
    public static PostCategory fromValue(String value) {
        for (PostCategory category : values()) {
            if (category.getValue().equals(value)) {
                return category;
            }
        }
        throw new IllegalArgumentException("未知的帖子分类: " + value);
    }
    
    /**
     * 通过描述查找枚举
     */
    public static PostCategory fromDesc(String desc) {
        for (PostCategory category : values()) {
            if (category.getDesc().equals(desc)) {
                return category;
            }
        }
        throw new IllegalArgumentException("未知的帖子分类描述: " + desc);
    }
    
    /**
     * 检查值是否有效
     */
    public static boolean isValid(String value) {
        for (PostCategory category : values()) {
            if (category.getValue().equals(value)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取所有可选值
     */
    public static String[] getAllValues() {
        PostCategory[] categories = values();
        String[] values = new String[categories.length];
        for (int i = 0; i < categories.length; i++) {
            values[i] = categories[i].getValue();
        }
        return values;
    }
    
    /**
     * 获取所有描述
     */
    public static String[] getAllDescs() {
        PostCategory[] categories = values();
        String[] descs = new String[categories.length];
        for (int i = 0; i < categories.length; i++) {
            descs[i] = categories[i].getDesc();
        }
        return descs;
    }
    
    /**
     * 获取分类选项列表（用于前端下拉框）
     */
    public static List<Map<String, String>> getCategoryOptions() {
        List<Map<String, String>> options = new ArrayList<>();
        for (PostCategory category : values()) {
            Map<String, String> option = new HashMap<>();
            option.put("value", category.getValue());
            option.put("label", category.getDesc());
            options.add(option);
        }
        return options;
    }
    
    @Override
    public String toString() {
        return value;
    }
}